load("@fbcode//buck2:buck_rust_binary.bzl", "buck_rust_binary")
load("@fbcode_macros//build_defs:native_rules.bzl", "buck_filegroup", "buck_genrule")
load("@fbcode_macros//build_defs:rust_library.bzl", "rust_library")
load("@fbsource//tools/build_defs:glob_defs.bzl", "glob")

oncall("buck2")

buck_rust_binary(
    name = "starlark-bin",
    srcs = glob(
        ["bin/**/*.rs"],
    ),
    crate_root = "bin/main.rs",
    deps = [
        "fbsource//third-party/rust:anyhow",
        "fbsource//third-party/rust:argfile",
        "fbsource//third-party/rust:clap",
        "fbsource//third-party/rust:debugserver-types",
        "fbsource//third-party/rust:itertools",
        "fbsource//third-party/rust:lsp-types",
        "fbsource//third-party/rust:serde",
        "fbsource//third-party/rust:serde_json",
        "fbsource//third-party/rust:thiserror",
        "fbsource//third-party/rust:walkdir",
        ":starlark",
        "//buck2/gazebo/dupe:dupe",
    ],
)

# Runs lalrpop on grammar.lalrpop to generate grammar.rs
buck_genrule(
    name = "grammar",
    srcs = ["src/syntax/grammar.lalrpop"],
    out = ".",
    # We can't use $SRCS, because in Buck1 that is absolute, and lalrpop --out-dir only works with relative sources
    cmd = "$(exe fbsource//third-party/rust:lalrpop-lalrpop) src/syntax/grammar.lalrpop --out-dir $OUT",
)

buck_filegroup(
    name = "testcases",
    srcs = glob([
        "src/tests/bc/golden/*",
        "testcases/**",
    ]),
)

rust_library(
    name = "starlark",
    srcs = glob(["src/**/*.rs"]),
    doctests = False,  # FIXME
    env = {
        # Allows src/syntax/mod.rs to include grammar.rs generated by lalrpop.
        "OUT_DIR": "$(location :grammar)/src",
    },
    rustc_flags = [
        "--cfg=rust_nightly",
    ],
    test_deps = [
        "fbsource//third-party/rust:rand",
        # This is a hack to build `:testcases` when invoking a test.
        # Otherwise `OUT_DIR` is set, but it points to non-existing directory.
        # This is likely a bug in `rust_test` rule.
        ":testcases",
    ],
    test_env = {
        # Some of our tests include testcase files relative to CARGO_MANIFEST_DIR.
        # This is a hack that allows both `cargo test` and `buck test` to work.
        "CARGO_MANIFEST_DIR": "$(location :testcases)",
        "OUT_DIR": "$(location :grammar)/src",
    },
    deps = [
        "fbsource//third-party/rust:annotate-snippets",
        "fbsource//third-party/rust:anyhow",
        "fbsource//third-party/rust:bumpalo",
        "fbsource//third-party/rust:debugserver-types",
        "fbsource//third-party/rust:derivative",
        "fbsource//third-party/rust:derive_more",
        "fbsource//third-party/rust:either",
        "fbsource//third-party/rust:erased-serde",
        "fbsource//third-party/rust:fancy-regex",
        "fbsource//third-party/rust:hashbrown",
        "fbsource//third-party/rust:inventory",
        "fbsource//third-party/rust:itertools",
        "fbsource//third-party/rust:lalrpop-util",
        "fbsource//third-party/rust:logos",
        "fbsource//third-party/rust:lsp-server",
        "fbsource//third-party/rust:lsp-types",
        "fbsource//third-party/rust:maplit",
        "fbsource//third-party/rust:memchr",
        "fbsource//third-party/rust:memoffset",
        "fbsource//third-party/rust:num-bigint",
        "fbsource//third-party/rust:num-traits",
        "fbsource//third-party/rust:once_cell",
        "fbsource//third-party/rust:paste",
        "fbsource//third-party/rust:regex",
        "fbsource//third-party/rust:rustyline",
        "fbsource//third-party/rust:serde",
        "fbsource//third-party/rust:serde_json",
        "fbsource//third-party/rust:static_assertions",
        "fbsource//third-party/rust:strsim",
        "fbsource//third-party/rust:textwrap",
        "fbsource//third-party/rust:thiserror",
        "//buck2/allocative/allocative:allocative",
        "//buck2/gazebo/display_container:display_container",
        "//buck2/gazebo/dupe:dupe",
        "//buck2/starlark-rust/starlark_derive:starlark_derive",
        "//buck2/starlark-rust/starlark_map:starlark_map",
    ],
)
